package me.zfz.business.controller.impl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import me.zfz.base.bean.SmartResponse;
import me.zfz.base.constant.IConstant;
import me.zfz.business.bean.UserInfo;
import me.zfz.business.bean.entity.TNLoginLog;
import me.zfz.business.constant.IActionConstant;
import me.zfz.business.controller.base.BaseController;
import me.zfz.base.helper.HttpRequestHelper;
import me.zfz.business.service.LoginLogService;
import me.zfz.business.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.mixsmart.enums.YesNoType;
import com.mixsmart.utils.LoggerUtils;
import com.mixsmart.utils.StringUtils;

import eu.bitwalker.useragentutils.UserAgent;

import java.util.Objects;

/**
 * 登录
 *
 * @author author
 */
@Controller
@RequestMapping("/login")
public class LoginController extends BaseController {

    @Autowired
    private UserService userServ;
    @Autowired
    private LoginLogService loginLogServ;

    @RequestMapping(method = RequestMethod.GET)
    public String index() throws Exception {
        return IActionConstant.LOGIN;
    }

    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView checkLogin(HttpServletRequest request, ModelAndView model,
                                   String userName, String password, String code) throws Exception {
        boolean is = false;
        boolean isAdmin = true;
        String msg = null;
        HttpSession session = request.getSession();
        String screenWidth = request.getParameter("screenWidth");
        String screenHeight = request.getParameter("screenHeight");
        String resolution = request.getParameter("resolution");
        if (StringUtils.isNotEmpty(userName)
                && StringUtils.isNotEmpty(password)
                && StringUtils.isNotEmpty(code)) {
            //记录登录日志
            TNLoginLog loginLog = new TNLoginLog();
            String userAgentStr = request.getHeader("User-Agent");
            loginLog.setUserAgent(userAgentStr);
            UserAgent userAgent = UserAgent.parseUserAgentString(userAgentStr);
            loginLog.setBrowser(userAgent.getBrowser().getName());
            loginLog.setBrowserVersion(userAgent.getBrowserVersion().getVersion());
            loginLog.setOs(userAgent.getOperatingSystem().getName());
            loginLog.setDeviceType(userAgent.getOperatingSystem().getDeviceType().getName());
            loginLog.setIp(HttpRequestHelper.getIP(request));
            loginLog.setState(YesNoType.NO.getValue());
            loginLog.setResolution(resolution);
            if (StringUtils.isNotEmpty(screenWidth) && StringUtils.isNum(screenWidth)) {
                loginLog.setClientScreenWidth(Float.parseFloat(screenWidth));
            }
            if (StringUtils.isNotEmpty(screenHeight) && StringUtils.isNum(screenHeight)) {
                loginLog.setClientScreenHeight(Float.parseFloat(screenHeight));
            }
            Object codeStr = session.getAttribute(IActionConstant.SESSION_CAPTCHA_LOGIN);
            UserInfo userInfo = null;
            if (null != codeStr && StringUtils.isNotEmpty(codeStr.toString())
                    && codeStr.toString().equalsIgnoreCase(code)) {
                SmartResponse<UserInfo> smartResp = userServ.login(userName, password);
                if (IConstant.OP_SUCCESS.equals(smartResp.getResult())) {
                    userInfo = smartResp.getData();
                    if (Objects.equals(IConstant.OP_SUCCESS, userInfo.getAuditFlag())) {
                        setUserInfo2Session(session, userInfo);
                        is = true;
                        loginLog.setState(YesNoType.YES.getValue());
                        loginLog.setUsername(userInfo.getUsername());
                        loginLog.setUserId(userInfo.getId());
                        isAdmin = !Objects.equals("01337e1d2666412fa3d028136ff710f7", userInfo.getRoleIds().get(0));
                        msg = "登录成功 ";
                    } else {
                        msg = "用户暂未审核，请联系上级进行审核";
                    }
                } else {
                    msg = "用户名或密码输入错误";
                }
            } else {
                msg = "验证码输入错误";
            }
            loginLog.setMsg(msg);
            loginLogServ.save(loginLog);
            LoggerUtils.debug(log, "保存登录日志成功");
            LoggerUtils.debug(log, "登录结果：" + msg);
            if (null != userInfo) {
                userInfo.setLoginId(loginLog.getId());
            }
        }
        if (is) {
            RedirectView view = new RedirectView(isAdmin ? "/index" : "/showPage/front_index", true, true, false);
            model.setView(view);
        } else {
            ModelMap modelMap = model.getModelMap();
            modelMap.put("userName", userName);
            modelMap.put("password", password);
            modelMap.put("code", code);
            modelMap.put("msg", msg);
        }
        return model;
    }

}
